<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>插入排序</title>
</head>
<body>
    <script>

        var arr = [23,545,65,78,2,56,45,7,4,34,56]
        function insertSort(arr){
            //思想局部有序
            var length = arr.length;
            for(var i=1;i<length;i++){
                var temp = arr[i];
                var j=i-1;//默认已排序的元素 
                while(j>=0 && arr[j]>temp){//条件不成立一次向后位移
                    arr[j+1]=arr[j]; //已排序的元素大于新元素，将该元素移到一下个位置
                    j--
                }
                arr[j+1]=temp; //插到temp 前面
            }
            return arr;
        }
        console.log(insertSort(arr))
    </script>
</body>
</html>